package org.company.management.ui;

import org.company.management.controller.EmployeeGroupJpaController;
import org.company.management.controller.EmployeeInfoJpaController;
import org.company.management.controller.ParseController;
import org.company.management.controller.StatusLineController;
import org.company.management.controller.UiController;
import org.company.management.entity.BehaviourEnum;
import org.company.management.entity.Employee;
import org.company.management.entity.EmployeeGroup;
import org.company.management.entity.EmployeeInfo;
import org.company.management.utilities.Strings;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.EditorPart;
import org.eclipse.wb.swt.ResourceManager;
import org.eclipse.wb.swt.layout.grouplayout.GroupLayout;
import org.eclipse.wb.swt.layout.grouplayout.LayoutStyle;

/**
 * @author Paner
 *
 */
public class EmployeeCardEditor extends EditorPart implements SelectionListener {
	
	public final static String ID = Strings.employeeCardEditorId;
	
	private Group _employeesGroup;
	private Group _employeesperformanceGroup;
	private Button _backButton;
	private Text _nameText;
	private Text _lastNameText;
	private Text _phoneText;
	private Combo _groupCombo;
	private Employee _employee;
	private Text _penaltyText;
	private Text _commentText;
	private Combo _behaviourCombo;
	private Button _minusButton;
	private Button _plusButton;
	private Button _updateButton;
	private Label _imageLabel;
	
	/* 
	 * Constructor, sets title image for editor
	 */
	public EmployeeCardEditor() {
		setTitleImage(ResourceManager.getPluginImage(Strings.pluginId, Strings.cardUserPicture16x16));
		
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		// TODO Auto-generated method stub

	}

	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		try
		{
			EditorInput inp = (EditorInput)input;
			_employee = (Employee)inp.getParticipant();
		}
		catch(Exception ex)
		{
			throw new RuntimeException("Wrong input");
		}
		setSite(site);
		setInput(input);

	}

	@Override
	public boolean isDirty() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void createPartControl(Composite parent) {
		createControls(parent);
		createLayout(parent);
		fillEditor();

	}
	
	/*
	 * Creates widgets
	 */
	private void createControls(Composite parent) {
		
		_employeesGroup = new Group(parent, SWT.NONE);
		_employeesGroup.setText(Strings.employee);
		
		_groupCombo = new Combo(_employeesGroup, SWT.READ_ONLY);
		_groupCombo.setEnabled(false);
		_groupCombo.setBounds(114, 220, 239, 23);
		_groupCombo.addSelectionListener(this);
		
		_employeesperformanceGroup = new Group(parent, SWT.NONE);
			
		_behaviourCombo = new Combo(_employeesperformanceGroup, SWT.READ_ONLY);
		_behaviourCombo.setBounds(112, 51, 239, 23);
		
		_backButton = new Button(parent, SWT.NONE);
		_backButton.setToolTipText(Strings.backTooltip);
		_backButton.setImage(ResourceManager.getPluginImage(Strings.pluginId, Strings.backButtonPicture32x32));
		_backButton.addSelectionListener(this);
		
		_minusButton = new Button(_employeesperformanceGroup, SWT.NONE);	
		_minusButton.setBounds(112, 113, 37, 25);
		_minusButton.addSelectionListener(this);
		
		_plusButton = new Button(_employeesperformanceGroup, SWT.NONE);
		_plusButton.setBounds(314, 113, 37, 25);
		_plusButton.addSelectionListener(this);
		
		_updateButton = new Button(parent, SWT.NONE);
		_updateButton.setToolTipText(Strings.confirmTooltip);
		_updateButton.setImage(ResourceManager.getPluginImage(Strings.pluginId, Strings.confirmButtonPicture32x32));
		_updateButton.addSelectionListener(this);
		
		_nameText = new Text(_employeesGroup, SWT.BORDER);
		_nameText.setEditable(false);
		_nameText.setBounds(114, 63, 239, 21);
		
		_lastNameText = new Text(_employeesGroup, SWT.BORDER);
		_lastNameText.setEditable(false);
		_lastNameText.setBounds(114, 116, 239, 21);
		
		_phoneText = new Text(_employeesGroup, SWT.BORDER);
		_phoneText.setEditable(false);
		_phoneText.setBounds(114, 171, 239, 21);
		
		_penaltyText = new Text(_employeesperformanceGroup, SWT.BORDER);
		_penaltyText.setEditable(false);
		_penaltyText.setBounds(182, 115, 108, 21);
		
		_commentText = new Text(_employeesperformanceGroup, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		_commentText.setBounds(112, 174, 239, 67);
		
		Label nameLabel = new Label(_employeesGroup, SWT.NONE);
		nameLabel.setText(Strings.name);
		nameLabel.setBounds(29, 66, 55, 15);
		
		Label lastnameLabel = new Label(_employeesGroup, SWT.NONE);
		lastnameLabel.setText(Strings.lastName);
		lastnameLabel.setBounds(29, 119, 55, 15);
		
		Label phoneLabel = new Label(_employeesGroup, SWT.NONE);
		phoneLabel.setText(Strings.phone);
		phoneLabel.setBounds(29, 174, 79, 15);
		
		Label employeesgroupLabel = new Label(_employeesGroup, SWT.NONE);
		employeesgroupLabel.setText(Strings.group);
		employeesgroupLabel.setBounds(29, 228, 55, 15);
		
		Label employeeperformanceLabel = new Label(_employeesperformanceGroup, SWT.NONE);
		employeeperformanceLabel.setText(Strings.performance);
		employeeperformanceLabel.setBounds(30, 54, 55, 15);
		
		_imageLabel = new Label(_employeesGroup, SWT.NONE);
		_imageLabel.setBounds(389, 63, 172, 89);
		
		Label penaltyLabel = new Label(_employeesperformanceGroup, SWT.NONE);
		penaltyLabel.setText(Strings.penalty);
		penaltyLabel.setBounds(30, 118, 55, 15);
		
		Label commentsCombo = new Label(_employeesperformanceGroup, SWT.NONE);
		commentsCombo.setText(Strings.comments);
		commentsCombo.setBounds(30, 191, 89, 15);
		
	}
	
	private void createLayout(Composite parent)
	{
		GroupLayout gl_parent = new GroupLayout(parent);
		gl_parent.setHorizontalGroup(
			gl_parent.createParallelGroup(GroupLayout.LEADING)
				.add(gl_parent.createSequentialGroup()
					.addContainerGap()
					.add(_backButton)
					.add(28)
					.add(_employeesGroup, GroupLayout.DEFAULT_SIZE, 571, Short.MAX_VALUE)
					.add(10))
				.add(GroupLayout.TRAILING, gl_parent.createSequentialGroup()
					.add(82)
					.add(_employeesperformanceGroup, GroupLayout.DEFAULT_SIZE, 571, Short.MAX_VALUE)
					.addContainerGap())
				.add(GroupLayout.TRAILING, gl_parent.createSequentialGroup()
					.addContainerGap(609, Short.MAX_VALUE)
					.add(_updateButton)
					.addContainerGap())
		);
		gl_parent.setVerticalGroup(
			gl_parent.createParallelGroup(GroupLayout.LEADING)
				.add(gl_parent.createSequentialGroup()
					.addContainerGap()
					.add(gl_parent.createParallelGroup(GroupLayout.LEADING)
						.add(_backButton)
						.add(gl_parent.createSequentialGroup()
							.add(_employeesGroup, GroupLayout.PREFERRED_SIZE, 255, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(LayoutStyle.RELATED)
							.add(_employeesperformanceGroup, GroupLayout.PREFERRED_SIZE, 273, GroupLayout.PREFERRED_SIZE)))
					.addPreferredGap(LayoutStyle.RELATED)
					.add(_updateButton)
					.addContainerGap(25, Short.MAX_VALUE))
		);
		parent.setLayout(gl_parent);
	}

	/*
	 * Fills the components of the editor
	 */
	private void fillEditor()
	{
		try
		{
			_nameText.setText(_employee.getName());
			_lastNameText.setText(_employee.getLastname());
			_phoneText.setText(_employee.getPhone());
			
			//Set image
			if( _employee.getPicture() != null )
			{
				Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
				Image employeeImage = new Image(shell.getDisplay(), _employee.getPicture());  
				
				_imageLabel.setImage(employeeImage);
			}
			
			//Fill combo with values
			EmployeeGroupJpaController employeeGroupDao = new EmployeeGroupJpaController();
			_groupCombo.add("");		
			
			//Fill combo with groups
			for(EmployeeGroup employeeGroup : employeeGroupDao.findEmployeeGroupEntities())
			{
				_groupCombo.add(employeeGroup.getName());
			}
			
			
			if(_employee.getGroupid() != null)
			{
				_groupCombo.setText(_employee.getGroupid().getName());
			}
			
			//Combobox for employees behaviour (good, normal, bad)
			_behaviourCombo.add("");
			_behaviourCombo.add(BehaviourEnum.GOOD.toString());
			_behaviourCombo.add(BehaviourEnum.NORMAL.toString());
			_behaviourCombo.add(BehaviourEnum.BAD.toString());

			if(_employee.getEmployeeInfo() != null)
			{
				_behaviourCombo.setText(_employee.getEmployeeInfo().getBehavior());
				_penaltyText.setText(_employee.getEmployeeInfo().getPenalty().toString());
				_commentText.setText(_employee.getEmployeeInfo().getComments().toString());
			}
			
		}
		catch(Exception ex)
		{
			//throw;
			ex.printStackTrace();
		}
	}

	@Override
	public void widgetSelected(SelectionEvent e) {
		Object selectedWidget = e.getSource();
		if(selectedWidget == _backButton)
		{
			UiController.showEditor(Strings.userData, EmployeeGalleryEditor.ID);
		}
		
		//Decrease penalties
		else if (selectedWidget == _minusButton)
		{
			
		}
		//Increase penalties
		else if (selectedWidget == _plusButton)
		{
			
		}
		//Set employee performance data
		else if (selectedWidget == _updateButton)
		{
			try
			{
				EmployeeInfo employeeInfo = new EmployeeInfo();
				employeeInfo.setEmployeeid(_employee);
				employeeInfo.setBehavior(_behaviourCombo.getText());
				if(ParseController.tryParse(_penaltyText.getText()) != null)
				{
					employeeInfo.setPenalty(Integer.parseInt(_penaltyText.getText()));
				}
				else
				{
					employeeInfo.setPenalty(0);
				}
				
				employeeInfo.setComments(_commentText.getText());
				
				EmployeeInfoJpaController employeeInfoDao = new EmployeeInfoJpaController();
				employeeInfoDao.create(employeeInfo);
				
				StatusLineController.setStatusLine(getEditorSite(), Strings.validUpdatedInputMessage);

			}
			catch (Exception ex)
			{
				ex.printStackTrace();
			}
		}
		
	}

	@Override
	public void widgetDefaultSelected(SelectionEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

}
